<?php

namespace app\index\controller;

use think\Request;

/**
 * 玩家个人
 */
class User extends Base
{
    // 返回 玩家列表
    public function index()
    {
        $map = [];
        $map['status'] = 1;
        if (is_numeric(input('param.status'))) {
            $map['status'] = input('param.status');
        }
        if (input('param.pid')) {
            $map['pid'] = input('param.pid');
        }
        if (input('param.keywords')) {
            $map['name|tel'] = input('param.keywords');
        }
        if (is_numeric(input('param.is_agent'))) {
            $map['is_agent'] = input('param.is_agent');
        }
        if (is_numeric(input('param.is_shop'))) {
            $map['is_shop'] = input('param.is_shop');
        }
        if (input('param.p_code')) {
            $map['province'] = input('param.p_code');
        }
        if (input('param.c_code')) {
            $map['city'] = input('param.c_code');
        }

        $list = db('user')->where($map)->paginate(input('param.size'));
        $this->result($list);
    }

    // 获取团队
    public function getTeam()
    {
        $map['status'] = 1;
        if (input('param.pid')) {
            $map['pid'] = input('param.pid');
        }

        $list = model('User')->getTeam($map, input('param.size'));
        $this->result($list);
    }

    // 获取账户明细
    public function getAcclog()
    {
        $map['uid'] = $this->user['uid'];

        if (input('param.acc_type')) {
            $map['acc_type'] = input('param.acc_type');
        }

        $list = model('UserAccountRecords')->getList($map, input('param.size'));

        $this->result($list);
    }

    // 获取玩家信息
    public function getInfo()
    {
        if (input('post.tel')) {
            $uid = db('user')->where('tel', input('post.tel'))->value('uid');
        }
        $uid = isset($uid) ? $uid : $this->user['uid'];

        $info = model('User')->getInfo($uid);
        $this->result($info);
    }

    // 会员信息修改
    public function editInfo()
    {
        $post = input('post.');
        if (config('sms.status') && input('post.is_code')) {
            $smsCode = cache($post['mobile'].'code') ?: '';
            if ($smsCode != $post['code'] || $post['code'] == '') {
                $this->error('短信验证码无效！');
            }
        }

        $uid = $this->user['uid'];
        if ($post) {
            $rst  = model('User')->editInfo($post, $uid);
            $this->success($rst);
        }
        $this->error();
    }

    // 货币转换rules
    public function coinRules()
    {
        $list = db('user_coin_trans')->where('status', 1)->select();
        $this->result($list);
    }

    // 玩家账户货币转换
    public function coinTrans()
    {
        $post = input('post.');

        $validate = validate('Vuser');
        $rst = $validate->scene('cointrans')->check($post);
        if (! $rst) {
            $this->error($validate->getError());
        } else {
            $rst = model('User')->coinTrans($post, $this->user['uid']);
            $this->success($rst);
        }
        
        $this->error('刷新重试！');
    }

    // 申请成为商家
    public function toshop()
    {
        $post = input('post.');
        $rst = model('User')->beShop($post, $this->user['uid']);
        $this->success($rst);
    }

    // ------------------------------------------------------------------------
    //  转账、买入、卖出
    // ------------------------------------------------------------------------
    // 转账给代理（充值）
    public function userAgent()
    {   
        $post = input('post.');
        $post['type'] = 1;
        $post['uid'] = $this->user['uid'];
        $rst = model('UserAgent')->addInfo($post);
        $this->success($rst);
    }

    // 转账记录列表
    public function transList()
    {
        $map = [
            'type' => 1 //转账记录
        ];
        if (input('param.status')) {
            $map['status'] = input('param.status');
        }
        if (input('param.uid')) {
            $map['uid'] = input('param.uid');
        }
        if (input('param.agent_uid')) {
            $map['agent_uid'] = input('param.agent_uid');
        } else {
            $map['uid'] = input('param.uid');
        }

        $list = model('UserAgent')->getList($map, input('param.size'));
        $this->result($list);
    }
   
    // 卖出
    public function sell()
    {
        $post = input('post.');
        if ($post){
            $rst = model('UserTrade')->addInfo($post, $this->user['uid']);
            $this->success($rst);
        }
    }
    
    // 卖出列表
    public function sellList()
    {   
        $map = [];
        if (input('param.seller_uid')) {
            $map['seller_uid'] = input('param.seller_uid');
        }
        if (input('param.buyer_uid')) {
            $map['buyer_uid'] = input('param.buyer_uid');
        }
        if (is_numeric(input('param.status'))) {
            $map['status'] = input('param.status');
        }
        if (input('param.trade_no')) {
            $map['trade_no'] = ['like', input('param.trade_no').'%'];
        }

        $list = model('UserTrade')->getList($map, input('param.size'));
        $this->result($list);
    }

    // 取消卖出
    public function cancleSell()
    {
        // 交易单号ID
        $id = input('param.id');

        if (encrypt(input('param.s_pwd')) != $this->user['s_pwd']) {
            $this->error('密码有错');
        }

        $rst = model('UserTrade')->reject($id, '会员：'.$this->user['name'].'主动', $this->user['uid']);
        $this->success($rst);
    }
    
    // 买入
    public function buy()
    {
        $post = input('post.');
        $rst = model('UserTrade')->addBuyer($post);

        $this->success($rst);
    }

    // 买入记录 1 待卖家确认，2 交易完成
    public function buyList()
    {
        $map = [];
        $map['buyer_uid'] = $this->user['uid'];
        if (input('param.buyer_uid')) {
            $map['buyer_uid'] = input('param.buyer_uid');
        }
        if (is_numeric(input('param.status'))) {
            $map['status'] = input('param.status');
        }

        $list = model('UserTrade')->getList($map, input('param.size'));
        $this->result($list);
    }

    // 买入确认
    public function sellConfirm()
    {
        $post = input('post.');

        $post['pwd'] = $this->user['s_pwd'];

        $rst = model('UserTrade')->confirm($post);
        $this->success($rst);
    }

    // ------------------------------------------------------------------------
    // 获取短信验证码
    public function smsCode()
    {
        $tel = input('post.mobile');
        if (! $tel) {
            $this->error('手机号码为空');
        }
        if (cache($tel.'code')) {
            $this->error('请勿重复发送');
        }

        $sms = config('sms');

        $sms['password'] = ucfirst(md5($sms['password']));
        $sms['mobile'] = $tel;

        $code = mt_rand(1234, 9876);
        $sms['content']  .= $code;
        $sms['sendTime'] = '';

        $query_str = http_build_query($sms);

        $gateway = "http://114.113.154.5/sms.aspx?action=send&".$query_str;
        $result = file_get_contents($gateway);
        $xml = simplexml_load_string($result);

        if ($xml->returnstatus == 'Faild') {
            $this->error($xml->message);
        }
        cache($tel.'code', $code, 120);
        $this->success('验证码'.$code.'已发送,两分钟内有效');
    }

    //-------------------------------------------------------------------------
    // 留言列表
    public function noteList()
    {   
        $map['uid'] = $this->user['uid'];
        $list = model('Message')->getList($map, input('param.size'));
        $this->result($list);
    }

    // 留言
    public function leaveNote()
    {
        $post = input('post.');
        $post['uid'] = $this->user['uid'];
        
        $rst = model('Message')->addInfo($post);
        $this->success($rst);
    }

    //-------------------------------------------------------------------------
    // 会员注册
    public function reg()
    {
        $post = input('post.');
        if (config('sms.status')) {
            $smsCode = cache($post['tel'].'code') ?: '';
            if ($smsCode != $post['code'] || $post['code'] == '') {
                $this->error('短信验证码无效！');
            }
        }
       
        $validate = validate('Vuser');
        $rst = $validate->scene('addUser')->check($post);

        if (! $rst) {
            $this->error($validate->getError());
        } else {
            if (isset($post['p_tel'])) {
                $pid = db('user')->where('tel', $post['p_tel'])->value('uid');
                if ($pid) {
                    $post['pid'] = $pid;
                } else {
                    $this->error('推荐人不存在！');
                }
            }
            $ret = model('User')->addInfo($post);
            $this->success($ret);
        }
        $this->error('稍后重试！');
    }

    // 登录
    public function login()
    {
        $this->result($this->user);
    }

}
